package com.lyl.goods.controller;

import com.lyl.goods.pojo.Product;
import com.lyl.goods.pojo.ShoppingCart;
import com.lyl.goods.service.CartService;
import com.lyl.goods.service.ProductService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Author: Liyunlong
 * @Date: 2024/03/01/19:08
 * @Description:
 */
@Controller
@Slf4j
public class CartController {
    @Autowired
    private ProductService productService;
    @Autowired
    private CartService cartService;
    /**
    * @Description: 根据用户id展示购物车
    * @Author: Liyunlong
    * @Date: 2024/3/2
    */
    @GetMapping("/cartList")
    public String showCartForm(HttpSession session, Model model) {

        String currentUser = (String) session.getAttribute("currentUser");
        model.addAttribute("userName", currentUser);
//
        Integer userId = (Integer) session.getAttribute("userId");
        List<ShoppingCart> shoppingCart = cartService.cartList(userId);
        // 创建一个列表，存储购物车每一项的信息，包括商品名、价格、数量及总价
        List<Map<String, Object>> cartItemList = new ArrayList<>();
        for (ShoppingCart cart : shoppingCart) {
            Integer productId = cart.getProductId();
            List<Product> productList = productService.getProduct(productId);
            if (!productList.isEmpty()) {
                Product product = productList.get(0);
                Map<String, Object> cartItemInfo = new HashMap<>();
                cartItemInfo.put("cartId", cart.getCartId());
                cartItemInfo.put("productName", product.getName());
                cartItemInfo.put("productPrice", product.getPrice());
                cartItemInfo.put("quantity", cart.getQuantity());
                // 计算总价
                cartItemInfo.put("totalPrice", product.getPrice().multiply(BigDecimal.valueOf(cart.getQuantity())));
                cartItemList.add(cartItemInfo);
            }
        }
        model.addAttribute("cartItemList", cartItemList);
        return "cartList";
    }
    /**
    * @Description: 加入购物车
    * @Author: Liyunlong
    * @Date: 2024/3/2
    */

    @RequestMapping("/addCart")
    public String addCart(@RequestParam("id") Long productId) {
        log.info("商品的ID:{}", productId);

        ShoppingCart shoppingCart = cartService.selectByProductId(productId);
        if (shoppingCart != null && shoppingCart.getCartId() != null) {
            // 如果购物车中存在该商品，则增加数量
            int currentQuantity = shoppingCart.getQuantity();
            shoppingCart.setQuantity(currentQuantity + 1);
            cartService.update(productId,shoppingCart);
        } else {
            // 如果购物车中不存在该商品，则添加新记录
            cartService.add(productId);
        }
        return "redirect:/cartList";
    }
    /**
    * @Description: 删除购物车
    * @Author: Liyunlong
    * @Date: 2024/3/2
    */
    @RequestMapping("/cart/deleteProduct")
    public String delete(@RequestParam("cartId") Long cartId){
        log.info("删除购物车中的商品：{}",cartId);
        cartService.delete(cartId);
        return "redirect:/cartList";
    }
    /**
    * @Description: 购物车商品+1
    * @Author: Liyunlong
    * @Date: 2024/3/2
    */
    @RequestMapping("/JiaProduct")
    public String addQuautity(@RequestParam("cartId") Long cartId){
        ShoppingCart shoppingCart=cartService.getByCartId(cartId);
        int quantity = shoppingCart.getQuantity();
        quantity=quantity+1;
        cartService.addQuantity(cartId,quantity);
        return "redirect:/cartList";
    }
    /**
    * @Description: 商品减1
    * @Author: Liyunlong
    * @Date: 2024/3/2
    */
    @RequestMapping("/JianProduct")
    public String JianQuautity(@RequestParam("cartId") Long cartId){
        ShoppingCart shoppingCart=cartService.getByCartId(cartId);
        int quantity = shoppingCart.getQuantity();
        quantity=quantity-1;
        cartService.JianQuantity(cartId,quantity);
        return "redirect:/cartList";
    }
}
